﻿using Factory.MasterData.Materials.Dto;
using Factory.WarehouseManagement.WarehouseSettings.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Factory.WarehouseManagement.WarehouseSettings
{
    public class ReservoirAreaAppService : ApplicationService, IReservoirAreaAppService
    {
        private readonly IRepository<ReservoirArea, int> _repository;
   

        public ReservoirAreaAppService(IRepository<ReservoirArea, int> repository)
        {
            _repository = repository;
        

        }

        public async Task<PageResult> GetListAsync(string areaName,string setCode, int pageIndex = 1, int pageSize = 10)
        {
            var setlist = await _repository.GetListAsync(x=>x.SetCode==setCode);
            
            setlist = setlist.WhereIf(!string.IsNullOrEmpty(areaName), x => x.AreaName.Contains(areaName)).ToList();
            PageResult pageResult = new PageResult();
            pageResult.RowCount = setlist.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = setlist.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;
        }





        public int CreateAsync(ReservoirArea input)
        {
            var warehouseSetlist = _repository.GetListAsync(x => x.AreaName == input.AreaName).Result;

            // 检查是否已存在该单位名称
            if (warehouseSetlist.Count() > 0)
            {
                return -100;  // 表示重复，或者抛出异常等
            }

            var warehouselist = _repository.InsertAsync(new ReservoirArea
            {
                AreaName = input.AreaName,
                AreaCapacity = input.AreaCapacity,
                AreaCode = input.AreaCode,
                IsArea = input.IsArea,
                SetCode = input.SetCode,
                ResultArea = input.ResultArea,           
            });
            return 1;
        }

        public async Task<int> UpdateAsync(ReservoirAreaDTO input)
        {
            var warehouselist = await _repository.FindAsync(x => x.Id == input.Id);
            if (warehouselist == null)
            {
                // 客户未找到，可以根据需要抛出异常或返回特定值
                return 0;
            }
            warehouselist.AreaName = input.AreaName;
       
            warehouselist.AreaCapacity = input.AreaCapacity;
            warehouselist.IsArea = input.IsArea;
            warehouselist.ResultArea = input.ResultArea;
            warehouselist.AreaCode = input.AreaCode;
            await _repository.UpdateAsync(warehouselist);
            return 1; // 或者返回其他表示成功的值
        }

        public async Task<int> DeleteAsync(int id)
        {
            var warehouselist = await _repository.FindAsync(x => x.Id == id);
            if (warehouselist != null)
            {
                await _repository.DeleteAsync(warehouselist);
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public ReservoirArea GetReservoirAreaById(int id)
        {
            var warehouselist = _repository.FindAsync(id).Result;
            return warehouselist;
        }
    }
}
